﻿using System;
using System.Collections.Generic;
using RuntimeGizmos;
using SizeboxUI;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000245 RID: 581
	public class HandleManager : MonoBehaviour
	{
		// Token: 0x17000175 RID: 373
		// (get) Token: 0x06000C69 RID: 3177 RVA: 0x00064E9C File Offset: 0x0006309C
		// (set) Token: 0x06000C6A RID: 3178 RVA: 0x00064EA4 File Offset: 0x000630A4
		public CharacterEditor TargetEditor { get; private set; }

		// Token: 0x17000176 RID: 374
		// (get) Token: 0x06000C6B RID: 3179 RVA: 0x00064EAD File Offset: 0x000630AD
		// (set) Token: 0x06000C6C RID: 3180 RVA: 0x00064EB5 File Offset: 0x000630B5
		public SkeletonEditHandle TargetHandle { get; private set; }

		// Token: 0x17000177 RID: 375
		// (get) Token: 0x06000C6D RID: 3181 RVA: 0x00064EBE File Offset: 0x000630BE
		// (set) Token: 0x06000C6E RID: 3182 RVA: 0x00064EC6 File Offset: 0x000630C6
		public List<SkeletonEditHandle> TargetHandles { get; private set; }

		// Token: 0x17000178 RID: 376
		// (get) Token: 0x06000C6F RID: 3183 RVA: 0x00064ECF File Offset: 0x000630CF
		// (set) Token: 0x06000C70 RID: 3184 RVA: 0x00064ED7 File Offset: 0x000630D7
		public Dictionary<string, SkeletonEditHandle> HandleMap { get; private set; }

		// Token: 0x17000179 RID: 377
		// (get) Token: 0x06000C71 RID: 3185 RVA: 0x00064EE0 File Offset: 0x000630E0
		// (set) Token: 0x06000C72 RID: 3186 RVA: 0x00064EE8 File Offset: 0x000630E8
		public Dictionary<SkeletonEditHandle, string> KeyMap { get; private set; }

		// Token: 0x06000C73 RID: 3187 RVA: 0x00064EF4 File Offset: 0x000630F4
		private void Awake()
		{
			this.TargetHandles = new List<SkeletonEditHandle>();
			this.HandleMap = new Dictionary<string, SkeletonEditHandle>();
			this.KeyMap = new Dictionary<SkeletonEditHandle, string>();
			this.mainCamera = Camera.main;
			this.uiController = this.mainCamera.GetComponent<InterfaceControl>();
			this.placement = this.uiController.GetComponent<EditPlacement>();
			this.gizmo = this.mainCamera.GetComponent<TransformGizmo>();
		}

		// Token: 0x06000C74 RID: 3188 RVA: 0x00064F60 File Offset: 0x00063160
		private void OnEnable()
		{
			this.TargetHandle = null;
			this.ClearTargetHandles();
		}

		// Token: 0x06000C75 RID: 3189 RVA: 0x00064F6F File Offset: 0x0006316F
		private void Update()
		{
			this.UpdateHandles();
		}

		// Token: 0x06000C76 RID: 3190 RVA: 0x00064F78 File Offset: 0x00063178
		private void UpdateHandles()
		{
			if (!this.handlesEnabled || this.skeletonEditOptions == null)
			{
				return;
			}
			this.UpdateTargetHandle();
			if (this.gizmo.type != this.previousType)
			{
				this.previousType = this.gizmo.type;
				foreach (SkeletonEditHandle skeletonEditHandle in this.TargetHandles)
				{
					skeletonEditHandle.UpdatePosition(this.gizmo.type);
				}
			}
			if (this.TargetHandle && this.ActiveDataId != null)
			{
				TransformationData data = this.TargetHandle.UpdateHandle(this.ActiveDataId, this.gizmo.type, this.skeletonEditOptions);
				foreach (SkeletonEditHandle skeletonEditHandle2 in this.TargetHandles)
				{
					if (skeletonEditHandle2 != this.TargetHandle)
					{
						skeletonEditHandle2.UpdateHandle(this.ActiveDataId, data, this.gizmo.type, this.skeletonEditOptions);
					}
				}
			}
			foreach (SkeletonEditHandle skeletonEditHandle3 in this.boneHandles)
			{
				if (skeletonEditHandle3.gameObject.activeInHierarchy)
				{
					skeletonEditHandle3.UpdatePosition(this.gizmo.type);
				}
			}
		}

		// Token: 0x06000C77 RID: 3191 RVA: 0x00065140 File Offset: 0x00063340
		private void UpdateTargetHandle()
		{
			bool mouseButtonDown = Input.GetMouseButtonDown(0);
			bool mouseButtonDown2 = Input.GetMouseButtonDown(2);
			bool key = Input.GetKey(this.multiselectKey);
			bool key2 = Input.GetKey(this.deselectKey);
			if (mouseButtonDown2)
			{
				this.gizmo.SetTarget(null);
				this.TargetHandle = null;
				this.ClearTargetHandles();
			}
			else if (mouseButtonDown)
			{
				if (key2)
				{
					Ray ray = this.mainCamera.ScreenPointToRay(Input.mousePosition);
					RaycastHit[] array = Physics.RaycastAll(ray, float.PositiveInfinity, Layers.auxMask);
					if (array.Length > 0)
					{
						foreach (RaycastHit raycastHit in array)
						{
							SkeletonEditHandle componentInParent = raycastHit.collider.transform.GetComponentInParent<SkeletonEditHandle>();
							if (this.DeselectHandle(componentInParent))
							{
								break;
							}
						}
					}
				}
				else if (!EventSystem.current.IsPointerOverGameObject())
				{
					Ray ray2 = this.mainCamera.ScreenPointToRay(Input.mousePosition);
					RaycastHit[] array3 = Physics.RaycastAll(ray2, float.PositiveInfinity, Layers.auxMask);
					if (array3.Length > 0)
					{
						if (this.previousCount == array3.Length && this.cycleTimer > Time.time)
						{
							if (this.previousIndex + 1 == array3.Length)
							{
								this.previousIndex = -1;
							}
							for (int j = this.previousIndex + 1; j < array3.Length; j++)
							{
								SkeletonEditHandle componentInParent2 = array3[j].collider.transform.GetComponentInParent<SkeletonEditHandle>();
								if (this.SelectHandle(componentInParent2, key))
								{
									this.previousIndex = j;
									break;
								}
							}
						}
						else
						{
							this.previousIndex = -1;
							foreach (RaycastHit raycastHit2 in array3)
							{
								SkeletonEditHandle componentInParent3 = raycastHit2.collider.transform.GetComponentInParent<SkeletonEditHandle>();
								if (this.SelectHandle(componentInParent3, key))
								{
									break;
								}
							}
						}
					}
					this.previousCount = array3.Length;
					this.cycleTimer = Time.time + 2f;
				}
			}
		}

		// Token: 0x06000C78 RID: 3192 RVA: 0x00065380 File Offset: 0x00063580
		public void SetTarget(CharacterEditor target)
		{
			this.TargetEditor = target;
		}

		// Token: 0x06000C79 RID: 3193 RVA: 0x0006538C File Offset: 0x0006358C
		public void DeleteTargets(bool deleteBone = false)
		{
			for (int i = 0; i < this.TargetHandles.Count; i++)
			{
				SkeletonEditHandle skeletonEditHandle = this.TargetHandles[i];
				EditBone editBone = skeletonEditHandle.EditBone;
				if (editBone)
				{
					Transform realTransform = editBone.RealTransform;
					editBone.Disable();
					UnityEngine.Object.Destroy(editBone.gameObject);
					if (deleteBone)
					{
						this.TargetEditor.DeleteTransform(editBone.Key);
					}
				}
			}
			this.ClearTargetHandles();
		}

		// Token: 0x06000C7A RID: 3194 RVA: 0x0006540C File Offset: 0x0006360C
		private bool SelectHandle(SkeletonEditHandle handle, bool multiSelect)
		{
			if (!handle || this.TargetHandles.Contains(handle))
			{
				return false;
			}
			if (multiSelect)
			{
				this.TargetHandles.Add(handle);
			}
			else
			{
				this.ClearTargetHandles();
				this.TargetHandles.Add(handle);
			}
			this.gizmo.SetTarget(handle.transform);
			this.TargetHandle = handle;
			handle.Selected = true;
			if (this.onHandleSelection != null)
			{
				this.onHandleSelection(handle);
			}
			if (handle.EditBone.RealTransform)
			{
				if (this.TargetHandles.Count == 1)
				{
					this.selectionText.text = handle.EditBone.RealTransform.name;
				}
				else
				{
					this.selectionText.text = string.Concat(new object[]
					{
						handle.EditBone.RealTransform.name,
						" (+",
						this.TargetHandles.Count - 1,
						")"
					});
				}
			}
			this.selectionText.color = this.SELECT_COLOR;
			return true;
		}

		// Token: 0x06000C7B RID: 3195 RVA: 0x0006553C File Offset: 0x0006373C
		private bool DeselectHandle(SkeletonEditHandle handle)
		{
			if (!handle || !this.TargetHandles.Contains(handle))
			{
				return false;
			}
			handle.Selected = false;
			this.TargetHandles.Remove(handle);
			this.selectionText.text = handle.EditBone.RealTransform.name;
			this.selectionText.color = this.DESELECT_COLOR;
			return true;
		}

		// Token: 0x06000C7C RID: 3196 RVA: 0x000655A8 File Offset: 0x000637A8
		private void ClearTargetHandles()
		{
			foreach (SkeletonEditHandle skeletonEditHandle in this.TargetHandles)
			{
				if (skeletonEditHandle)
				{
					skeletonEditHandle.Selected = false;
					if (this.onHandleDeselection != null)
					{
						this.onHandleDeselection(skeletonEditHandle);
					}
				}
			}
			this.TargetHandles.Clear();
			this.selectionText.text = string.Empty;
		}

		// Token: 0x06000C7D RID: 3197 RVA: 0x00065648 File Offset: 0x00063848
		public void EnableHandlesMode(string id, List<string> keys = null)
		{
			if (!this.TargetEditor)
			{
				return;
			}
			SkeletonEdit skeletonEdit = this.TargetEditor.SkeletonEdit;
			this.ActiveDataId = id;
			this.EnsureHandles(skeletonEdit);
			this.HandleMap.Clear();
			this.KeyMap.Clear();
			if (keys != null)
			{
				int num = 0;
				EntityBase component = this.TargetEditor.GetComponent<EntityBase>();
				foreach (string text in keys)
				{
					if (skeletonEdit.BoneMap.ContainsKey(text))
					{
						SkeletonEditHandle skeletonEditHandle = this.boneHandles[num];
						skeletonEditHandle.gameObject.SetActive(true);
						skeletonEditHandle.AssignBone(skeletonEdit.BoneMap[text], component);
						this.KeyMap.Add(skeletonEditHandle, text);
						this.HandleMap.Add(text, skeletonEditHandle);
						num++;
					}
				}
			}
			this.uiController.commandEnabled = false;
			this.placement.updateHandles = false;
			this.gizmo.overrideSpace = true;
			this.gizmo.space = TransformSpace.Local;
			this.handlesEnabled = true;
		}

		// Token: 0x06000C7E RID: 3198 RVA: 0x00065788 File Offset: 0x00063988
		public void EnableHandles(ICollection<string> keys)
		{
			if (!this.handlesEnabled)
			{
				return;
			}
			foreach (string key in keys)
			{
				if (this.HandleMap.ContainsKey(key))
				{
					this.HandleMap[key].gameObject.SetActive(true);
				}
			}
		}

		// Token: 0x06000C7F RID: 3199 RVA: 0x0006580C File Offset: 0x00063A0C
		public void DisableHandles(ICollection<string> keys)
		{
			foreach (string key in keys)
			{
				if (this.HandleMap.ContainsKey(key))
				{
					this.HandleMap[key].Disable(base.transform);
				}
			}
		}

		// Token: 0x06000C80 RID: 3200 RVA: 0x00065884 File Offset: 0x00063A84
		public void DisableHandlesMode()
		{
			foreach (SkeletonEditHandle skeletonEditHandle in this.boneHandles)
			{
				skeletonEditHandle.Disable(base.transform);
			}
			this.uiController.commandEnabled = true;
			this.placement.updateHandles = true;
			this.gizmo.overrideSpace = false;
			this.ClearTargetHandles();
			if (this.TargetEditor)
			{
				this.gizmo.SetTarget(this.TargetEditor.transform);
			}
			this.ActiveDataId = null;
			this.handlesEnabled = false;
		}

		// Token: 0x06000C81 RID: 3201 RVA: 0x00065944 File Offset: 0x00063B44
		public void SetHandleStyle(SkeletonEditHandle.HandleStyle style, ICollection<string> keys, bool oppositeStyleOthers = false)
		{
			if (oppositeStyleOthers)
			{
				SkeletonEditHandle.HandleStyle style2;
				if (style == SkeletonEditHandle.HandleStyle.Active)
				{
					style2 = SkeletonEditHandle.HandleStyle.Inactive;
				}
				else
				{
					style2 = SkeletonEditHandle.HandleStyle.Active;
				}
				foreach (SkeletonEditHandle skeletonEditHandle in this.HandleMap.Values)
				{
					skeletonEditHandle.SetStyle(style2);
				}
			}
			foreach (string key in keys)
			{
				if (this.HandleMap.ContainsKey(key))
				{
					this.HandleMap[key].SetStyle(style);
				}
			}
		}

		// Token: 0x06000C82 RID: 3202 RVA: 0x00065A24 File Offset: 0x00063C24
		public void SetHandleSize(float sizeMult)
		{
			foreach (SkeletonEditHandle skeletonEditHandle in this.boneHandles)
			{
				skeletonEditHandle.SetSize(sizeMult);
			}
		}

		// Token: 0x06000C83 RID: 3203 RVA: 0x00065A80 File Offset: 0x00063C80
		private void EnsureHandles(SkeletonEdit skeletonEdit)
		{
			for (int i = 0; i < this.boneHandles.Count; i++)
			{
				if (this.boneHandles[i] == null)
				{
					this.boneHandles.RemoveAt(i);
					i--;
				}
			}
			int count = skeletonEdit.BoneMap.Count;
			for (int j = this.boneHandles.Count; j < count; j++)
			{
				SkeletonEditHandle item = UnityEngine.Object.Instantiate<SkeletonEditHandle>(this.handlePrefab, base.transform);
				this.boneHandles.Add(item);
			}
		}

		// Token: 0x06000C84 RID: 3204 RVA: 0x00065B18 File Offset: 0x00063D18
		public void ResetAll(string id)
		{
			if (!this.TargetEditor)
			{
				return;
			}
			foreach (EditBone editBone in this.TargetEditor.SkeletonEdit.BoneMap.Values)
			{
				editBone.ResetAll(id);
			}
		}

		// Token: 0x06000C85 RID: 3205 RVA: 0x00065B94 File Offset: 0x00063D94
		public void ResetTarget(string id, SkeletonEditOptions options)
		{
			if (!this.TargetEditor || !this.TargetHandle)
			{
				return;
			}
			foreach (SkeletonEditHandle skeletonEditHandle in this.TargetHandles)
			{
				skeletonEditHandle.ResetBone(id, options);
				skeletonEditHandle.UpdatePosition(this.gizmo.type);
			}
		}

		// Token: 0x06000C86 RID: 3206 RVA: 0x00065C24 File Offset: 0x00063E24
		public void Reset(string id, SkeletonEditOptions options)
		{
			if (!this.TargetEditor || !this.TargetHandle)
			{
				return;
			}
			foreach (SkeletonEditHandle skeletonEditHandle in this.TargetHandles)
			{
				skeletonEditHandle.Reset(id, this.gizmo.type, options);
				skeletonEditHandle.UpdatePosition(this.gizmo.type);
			}
		}

		// Token: 0x06000C87 RID: 3207 RVA: 0x00065CC0 File Offset: 0x00063EC0
		public void SetOptions(SkeletonEditOptions options)
		{
			this.skeletonEditOptions = options;
		}

		// Token: 0x06000C88 RID: 3208 RVA: 0x00065CC9 File Offset: 0x00063EC9
		public SkeletonEditHandle GetHandle(string key)
		{
			if (this.HandleMap.ContainsKey(key))
			{
				return this.HandleMap[key];
			}
			return null;
		}

		// Token: 0x06000C89 RID: 3209 RVA: 0x00065CEA File Offset: 0x00063EEA
		public string GetHandleKey(SkeletonEditHandle handle = null)
		{
			if (handle == null)
			{
				handle = this.TargetHandle;
			}
			if (this.KeyMap.ContainsKey(handle))
			{
				return this.KeyMap[handle];
			}
			return null;
		}

		// Token: 0x06000C8A RID: 3210 RVA: 0x00065D20 File Offset: 0x00063F20
		public void PairBones()
		{
			if (this.TargetHandles.Count != 2)
			{
				return;
			}
			EditBone editBone = this.TargetHandles[0].EditBone;
			EditBone editBone2 = this.TargetHandles[1].EditBone;
			if (!editBone.SiblingBone && !editBone2.SiblingBone)
			{
				this.TargetEditor.SkeletonEdit.CreatePair(editBone, editBone2);
			}
			else if (editBone.SiblingBone == editBone2)
			{
				this.TargetEditor.SkeletonEdit.RemovePair(editBone, editBone2);
			}
		}

		// Token: 0x04001187 RID: 4487
		[Header("Prefabs")]
		[SerializeField]
		private SkeletonEditHandle handlePrefab;

		// Token: 0x04001188 RID: 4488
		[SerializeField]
		[Header("Required References")]
		private Text selectionText;

		// Token: 0x04001189 RID: 4489
		private Color SELECT_COLOR = Color.white;

		// Token: 0x0400118A RID: 4490
		private Color DESELECT_COLOR = Color.grey;

		// Token: 0x0400118B RID: 4491
		public HandleManager.OnHandleSelect onHandleSelection;

		// Token: 0x0400118C RID: 4492
		public HandleManager.OnHandleSelect onHandleDeselection;

		// Token: 0x0400118D RID: 4493
		private Camera mainCamera;

		// Token: 0x0400118E RID: 4494
		private InterfaceControl uiController;

		// Token: 0x0400118F RID: 4495
		private EditPlacement placement;

		// Token: 0x04001190 RID: 4496
		private TransformGizmo gizmo;

		// Token: 0x04001196 RID: 4502
		private SkeletonEditOptions skeletonEditOptions = new SkeletonEditOptions();

		// Token: 0x04001197 RID: 4503
		private List<SkeletonEditHandle> boneHandles = new List<SkeletonEditHandle>();

		// Token: 0x04001198 RID: 4504
		private bool handlesEnabled;

		// Token: 0x04001199 RID: 4505
		private string ActiveDataId;

		// Token: 0x0400119A RID: 4506
		private TransformType previousType;

		// Token: 0x0400119B RID: 4507
		private float cycleTimer;

		// Token: 0x0400119C RID: 4508
		private int previousCount;

		// Token: 0x0400119D RID: 4509
		private int previousIndex = -1;

		// Token: 0x0400119E RID: 4510
		private const float SELECTION_CYCLING_SECONDS = 2f;

		// Token: 0x0400119F RID: 4511
		private KeyCode multiselectKey = KeyCode.LeftShift;

		// Token: 0x040011A0 RID: 4512
		private KeyCode deselectKey = KeyCode.LeftControl;

		// Token: 0x02000246 RID: 582
		// (Invoke) Token: 0x06000C8C RID: 3212
		public delegate void OnHandleSelect(SkeletonEditHandle handle);
	}
}
